const mongoose = require('mongoose')
const Image = mongoose.model('Image')
const Comment = mongoose.model('Comment')

// const statsfn = async() =>{
//   const imageStats = await Image.aggregate()
//     .group({
//       _id: null,
//       numOfImages:{$sum: 1},
//       numOfLikes:{$sum:"$likes"},
//       numOfVisits:{$sum:"$visit"},
//     })
//   const numOfComments = await Comment.countDocuments()
//   return{
//     ...imageStats[0],
//     numOfComments
//   }
// }

const statsfn = () => Promise.all([
  Image.aggregate()
    .group({
      _id: null,
      numOfImages:{$sum: 1},
      numOfLikes:{$sum:"$likes"},
      numOfVisits:{$sum:"$visit"},
    }),
    Comment.countDocuments()
]).then(([obj1,numOfComments]) => ({
    ...obj1[0],numOfComments
}))

module.exports = statsfn